package com.framework.buss.bridge.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.framework.buss.bridge.model.Bridge;
import com.framework.buss.bridge.service.IBridgeService;
import com.framework.common.domain.ResultData;
import com.framework.common.util.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

 /**
 * @Description: 桥梁表
 * @Author: jeecg-boot
 * @Date:   2023-03-28
 * @Version: V1.0
 */
@Api(tags="桥梁表")
@RestController
@RequestMapping("/bridge")
@Slf4j
public class BridgeController {
	@Autowired
	private IBridgeService bridgeService;
	
	/**
	 * 分页列表查询
	 *
	 * @param bridge
	 * @param pageNo
	 * @param pageSize
	 * @return
	 */
	//@AutoLog(value = "桥梁表-分页列表查询")
	@ApiOperation(value="桥梁表-分页列表查询", notes="桥梁表-分页列表查询")
	@GetMapping(value = "/list")
	public ResultData<IPage<Bridge>> queryPageList(Bridge bridge,
												   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
												   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize) {
		LambdaQueryWrapper<Bridge> queryWrapper = new LambdaQueryWrapper<>();
		if (!StringUtils.isEmpty(bridge.getName())) {
			queryWrapper.like(Bridge::getName, bridge.getName());
		}
		Page<Bridge> page = new Page<Bridge>(pageNo, pageSize);
		IPage<Bridge> pageList = bridgeService.page(page, queryWrapper);
		return ResultData.data(pageList);
	}
	
	/**
	 *   添加
	 *
	 * @param bridge
	 * @return
	 */
	@ApiOperation(value="桥梁表-添加", notes="桥梁表-添加")
	//@RequiresPermissions("org.jeecg.biz.bridge:_bridge:add")
	@PostMapping(value = "/add")
	public ResultData<String> add(@RequestBody Bridge bridge) {
		bridgeService.save(bridge);
		return ResultData.success("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param bridge
	 * @return
	 */
	@ApiOperation(value="桥梁表-编辑", notes="桥梁表-编辑")
	//@RequiresPermissions("org.jeecg.biz.bridge:_bridge:edit")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public ResultData<String> edit(@RequestBody Bridge bridge) {
		bridgeService.updateById(bridge);
		return ResultData.success("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@ApiOperation(value="桥梁表-通过id删除", notes="桥梁表-通过id删除")
	//@RequiresPermissions("org.jeecg.biz.bridge:_bridge:delete")
	@DeleteMapping(value = "/delete")
	public ResultData<String> delete(@RequestParam(name="id",required=true) String id) {
		bridgeService.removeById(id);
		return ResultData.success("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@ApiOperation(value="桥梁表-批量删除", notes="桥梁表-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public ResultData<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.bridgeService.removeByIds(Arrays.asList(ids.split(",")));
		return ResultData.success("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@ApiOperation(value="桥梁表-通过id查询", notes="桥梁表-通过id查询")
	@GetMapping(value = "/queryById")
	public ResultData<Bridge> queryById(@RequestParam(name="id",required=true) String id) {
		Bridge bridge = bridgeService.getById(id);
		if(bridge==null) {
			return ResultData.error("未找到对应数据");
		}
		return ResultData.data(bridge);
	}

}
